package com.anmoyi.web.controller;


import com.alibaba.fastjson.JSON;
import com.anmoyi.common.AppError;
import com.anmoyi.common.Packet;
import com.anmoyi.common.StringUtil;
import com.anmoyi.common.TokenUtil;
import com.anmoyi.common.exception.TokenException;
import com.anmoyi.model.po.Custommassage;
import com.anmoyi.model.po.Custommassagecontent;
import com.anmoyi.model.po.User;
import com.anmoyi.service.CustommassageService;
import com.anmoyi.service.CustommassagecontentService;
import com.anmoyi.service.UserService;
import com.anmoyi.web.ao.CustommassageAO;
import com.anmoyi.web.ao.CustommassagecontentAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CustommassagecontentController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(CustommassagecontentController.class);


    @Autowired
    private CustommassagecontentService custommassagecontentService;


    @Autowired
    private UserService userService;


    @PostMapping(value = "/addCustommassagecontent")
    public String addCustommassagecontent(@RequestBody String requestString) {
        logger.info("自定义按摩手法内容列数是: ");
        logger.info(requestString);


        Packet packet = null;

        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("自定义按摩手法内容参数异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }


        CustommassagecontentAO custommassagecontentAO = null;
        try {
            //packet.getData() 为jsonobject
            custommassagecontentAO = JSON.parseObject(packet.getData().toString(), CustommassagecontentAO.class);
        } catch (Exception e) {
            logger.error("自定义按摩手法内容参数异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        if (StringUtil.isNullOrBlank(custommassagecontentAO.getCid())) {
            logger.error("自定义按摩手法内容参数异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("自定义按摩手法内容参数异常4", e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        User user = userService.getByPhone(phone);
        if (null == user) {
            logger.error("自定义按摩手法内容参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();

        Custommassagecontent custommassagecontent = new Custommassagecontent();
        custommassagecontent.setCid(custommassagecontentAO.getCid());
        custommassagecontent.setMassagetechnique(custommassagecontentAO.getMassagetechnique());
        custommassagecontent.setMassagetime(custommassagecontentAO.getMassagetime());

        custommassagecontentService.addCustommassagecontent(custommassagecontent);

        logger.info("自定义按摩手法内容参数异常6");

        return responseToClient(AppError.APP_OK);

    }

    @PostMapping(value = "/updateCustommassagecontent")
    public String updateCustommassagecontent(@RequestBody String requestString) {
        logger.info("自定义按摩手法内容列数是: ");
        logger.info(requestString);


        Packet packet = null;

        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("自定义按摩手法内容参数异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }


        CustommassagecontentAO custommassagecontentAO = null;
        try {
            //packet.getData() 为jsonobject
            custommassagecontentAO = JSON.parseObject(packet.getData().toString(), CustommassagecontentAO.class);
        } catch (Exception e) {
            logger.error("自定义按摩手法内容参数异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        if (StringUtil.isNullOrBlank(custommassagecontentAO.getId())) {
            logger.error("自定义按摩手法内容参数异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("自定义按摩手法内容参数异常4", e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        User user = userService.getByPhone(phone);
        if (null == user) {
            logger.error("自定义按摩手法内容参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();

        Custommassagecontent custommassagecontent = new Custommassagecontent();
        custommassagecontent.setId(custommassagecontentAO.getId());
        custommassagecontent.setCid(custommassagecontentAO.getCid());
        custommassagecontent.setMassagetechnique(custommassagecontentAO.getMassagetechnique());
        custommassagecontent.setMassagetime(custommassagecontentAO.getMassagetime());

        custommassagecontentService.updateByPrimaryKeySelective(custommassagecontent);

        logger.info("自定义按摩手法内容参数异常6");

        return responseToClient(AppError.APP_OK);

    }

    @PostMapping(value = "/deleteCustommassagecontent")
    public String deleteCustommassagecontent(@RequestBody String requestString) {
        logger.info("自定义按摩手法内容列数是: ");
        logger.info(requestString);


        Packet packet = null;

        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("自定义按摩手法内容参数异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }


        CustommassagecontentAO custommassagecontentAO = null;
        try {
            //packet.getData() 为jsonobject
            custommassagecontentAO = JSON.parseObject(packet.getData().toString(), CustommassagecontentAO.class);
        } catch (Exception e) {
            logger.error("自定义按摩手法内容参数异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        if (StringUtil.isNullOrBlank(custommassagecontentAO.getId())) {
            logger.error("自定义按摩手法内容参数异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("自定义按摩手法内容参数异常4", e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        User user = userService.getByPhone(phone);
        if (null == user) {
            logger.error("自定义按摩手法内容参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();

        custommassagecontentService.deleteByPrimaryKey(custommassagecontentAO.getId());

        logger.info("自定义按摩手法内容参数异常6");

        return responseToClient(AppError.APP_OK);

    }

    @PostMapping(value = "/queryCustommassagecontent")
    public String queryCustommassagecontent(@RequestBody String requestString) {
        logger.info("更新自定义按摩手法列数是: ");
        logger.info(requestString);
        Packet packet = null;
        try {
            packet = JSON.parseObject(requestString, Packet.class);
        } catch (Exception e) {
            logger.error("更新自定义按摩手法参数异常1\n" + e);
            return responseToClient(AppError.APP_JSON_INVALID_ERROR);
        }
        CustommassagecontentAO custommassagecontentAO = null;
        try {
            //packet.getData() 为jsonobject
            custommassagecontentAO = JSON.parseObject(packet.getData().toString(), CustommassagecontentAO.class);
        } catch (Exception e) {
            logger.error("自定义按摩手法内容参数异常2", e);
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        if (StringUtil.isNullOrBlank(custommassagecontentAO.getId())) {
            logger.error("自定义按摩手法内容参数异常3");
            return responseToClient(AppError.APP_ARGS_ERROR);
        }


        String phone = null;
        try {
            phone = TokenUtil.getEmail(packet.getToken());
        } catch (TokenException e) {
            logger.error("自定义按摩手法内容参数异常4", e);
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        User user = userService.getByPhone(phone);
        if (null == user) {
            logger.error("自定义按摩手法内容参数异常5");
            return responseToClient(AppError.APP_TOKEN_INVALID_ERROR);
        }

        this.token = user.getToken();
        List<Custommassagecontent> list = custommassagecontentService.selectBycid(custommassagecontentAO.getCid());
        logger.info("更新自定义按摩手法参数异常6");
        return responseToClientWithData(AppError.APP_OK, list);
    }
}
